//
//  ForThePaymentViewController.m
//  lmq
//
//  Created by apple on 2017/3/8.
//  Copyright © 2017年 yuanxin. All rights reserved.
//

#import "ForThePaymentViewController.h"
#import "MyAllOrderTableViewCell.h"
#import "ALLMyShopModel.h"
#import "AllMyOrderModel.h"
#import "MyAllOrderSectionCell.h"
#import "UIImageView+WebCache.h"
#import "payControlViewController.h"

@interface ForThePaymentViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *dict1;
@property (nonatomic, strong) NSMutableArray *arr;
@property (nonatomic, strong) NSString *money;
@property (nonatomic, strong) NSMutableArray *dataArrNew;



@end

@implementation ForThePaymentViewController
//待付款
- (void)viewDidLoad {
    [super viewDidLoad];
    [self creationDate];
    [self creationView];
    [self regeditCell];
    // Do any additional setup after loading the view.
}
- (void)creationView{
    UIButton *leftBtn=[UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame=CGRectMake(0, 0, (22), (22));
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftBtn setBackgroundImage:[UIImage imageNamed:@"back"] forState:UIControlStateSelected];
    [leftBtn addTarget:self action:@selector(leftNavItemClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarBtn=[[UIBarButtonItem alloc]initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem=leftBarBtn;
    self.navigationItem.title = @"待付款";
    self.tableView = [[UITableView alloc] init];
    self.tableView.frame = CGRectMake(0, 0, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT - 64 - 40);
    self.tableView.tableFooterView = [[UITableView alloc] initWithFrame:CGRectZero];
    
    self.tableView.backgroundColor = [UIColor whiteColor];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    
}
-(void)leftNavItemClick{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)regeditCell{
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MyAllOrderTableViewCell" bundle:nil] forCellReuseIdentifier:@"MyAllOrderTableViewCell"];
    
    //    [self.tableView registerNib:[UINib nibWithNibName:@"MyAllOrderSectionCell" bundle:nil] forHeaderFooterViewReuseIdentifier:@"MyAllOrderSectionCell"];
    
}
- (void)creationDate{
    
    _dataArr = [[NSMutableArray alloc]init];
    _dataArrNew = [[NSMutableArray alloc]init];
    NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
    NSString *tokenStr=[PublicFunction getConfigWithKey:@"token"];
    NSString *memberId = [PublicFunction getConfigWithKey:@"uid"];
    [productParams setValue:tokenStr forKey:@"token"];
    [productParams setValue:memberId forKey:@"member_id"];
    [productParams setValue:@"0" forKey:@"status"];
    
    
    [[MNetworkHandler sharedInstance]conURL:My_AllOrderForPayment
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue]==1) {
                                       
                                       self.dict1=[NSMutableArray arrayWithArray:[returnData objectForKey:@"data"]];
                                       
                                       self.arr = [NSMutableArray arrayWithArray:[_dict1 valueForKey:@"shop"]];
                                       // _dataArr = [dict1 valueForKey:@"shop"];
                                       
                                       
                                       for (int i = 0; i < (self.arr.count); i++) {
                                           [self.dataArr addObjectsFromArray:_arr[i]];
                                           
                                       }
                                       
                                       for (NSDictionary *dic in _dict1 ) {
                                           AllMyOrderModel *model = [[AllMyOrderModel alloc]init];
                                           [model setValuesForKeysWithDictionary:dic];
                                           [_dataArrNew addObject:model];
                                           NSLog(@"AAAAAAAAAAAAAAAAAA%@", model);
                                       }
                                       
                                       
                                       
                                       [self.tableView reloadData];
                                   }
                                   else {
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:5.0f];
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:10.0f];
                               }];
    
}
- (void)zj_viewDidLoadForIndex:(NSInteger)index {
    //    NSLog(@"%@",self.view);
    //    NSLog(@"%@", self.zj_scrollViewController);
    
    
}
- (void)zj_viewWillAppearForIndex:(NSInteger)index {
    // NSLog(@"viewWillAppear------");
    
    
}


- (void)zj_viewDidAppearForIndex:(NSInteger)index {
    // NSLog(@"viewDidAppear-----");
    
}

#pragma mark - ---------- Section的数量 ----------
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
//#pragma mark - ---------- Section的内容 ----------
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//
//    MyAllOrderSectionCell *sectionCell = [self.tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MyAllOrderSectionCell"
//                                      ];
//    return sectionCell;
//
//}
#pragma mark - ---------- 每个Section的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

//section 不随tableView滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.tableView)
        
    {
        
        CGFloat sectionHeaderHeight = 64; //header高度
        
        if (scrollView.contentOffset.y <= sectionHeaderHeight && scrollView.contentOffset.y >= 0) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
            
        } else if (scrollView.contentOffset.y >= sectionHeaderHeight) {
            
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
            
        }
        
    }
    
    
    
}


#pragma mark - ---------- Cell的数量 ----------
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
    
    
    
}

#pragma mark - ---------- 每个Cell的高度 ----------
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 280;
    
}
#pragma mark - ---------- 每个Cell的内容 ----------
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyAllOrderTableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"MyAllOrderTableViewCell" forIndexPath:indexPath];
    
    
    NSMutableDictionary *dic = self.dataArr[indexPath.row];
    cell.nameLabel.text = [dic objectForKey:@"shop_name"];
    NSString *strPrice = @"¥";
    cell.priceLabel.text = [strPrice stringByAppendingString:[dic objectForKey:@"price"]];
    NSString *strNum = @"×";
    
    
    //商品数量
    cell.numberLaberl.text = [strNum stringByAppendingString:[dic objectForKey:@"shop_num"]];
    
    
    
    //总价钱
    float a = [[dic objectForKey:@"price"] floatValue];
    float b = [[dic objectForKey:@"shop_num"] floatValue];
    float c = a*b;
    
    NSString *strPriceAll = [NSString stringWithFormat:@"%.2f", (float)c];
    
    
    cell.allPriceLabel.text = [strPrice stringByAppendingString:strPriceAll];
    
    
    NSMutableDictionary *dict55 = self.dict1[indexPath.row];
    
    cell.productionLabel.text = [dict55 objectForKey:@"number"];
    cell.productionLabel.hidden = YES;
    [PublicFunction saveConfig:[dict55 objectForKey:@"number"] forKey:@"all_number"];
    [PublicFunction saveConfig:[dict55 objectForKey:@"order_id"] forKey:@"order_id"];
    
    
//    NSString *strPic = @"http://lmq.quyum.cn/";
//    
//    NSString *str = [strPic stringByAppendingString:[dic objectForKey:@"shoppic"]];
    
    NSString *urlWare=[dic objectForKey:@"shoppic"];
    NSString *str=manage(AP_HOST, urlWare);
    
    NSString *strUrl = [str stringByReplacingOccurrencesOfString:@"\\" withString:@"/"];
    
    [cell.picImage sd_setImageWithURL:[NSURL URLWithString:strUrl]];
    cell.cancelButton.hidden = YES;
    
//    [cell.paymentButton addTarget:self action:@selector(paymentClick:) forControlEvents:UIControlEventTouchUpInside];

    [cell callBackEditBlock:^() {
        
        AllMyOrderModel *model = self.dataArrNew[indexPath.row];
        NSString *str = model.money;
        
        [self editWorkExperienceWithModel:str];
        
        
    }];
    
    return cell;
    
    
}
- (void)editWorkExperienceWithModel:(NSString *)str{

    payControlViewController *vc = [[payControlViewController alloc] init];
    vc.str = str;
    [self.navigationController pushViewController:vc animated:YES];


}


- (void)paymentClick:(id)sender{
    
    UIButton *button = (UIButton *)sender;
    UITableViewCell  *cell;
    cell = (UITableViewCell *)button.superview;
    
    NSMutableDictionary *productParams = self.dict1[[self.tableView indexPathForCell:cell].row];
    
    
    payControlViewController *payControlView = [[payControlViewController alloc]init];
    payControlView.str = [productParams objectForKey:@"money"];
    
    [self.navigationController pushViewController:payControlView animated:YES];
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
